#ifndef EVENTFACTORY_H
#define EVENTFACTORY_H

#include "../log.h"
#include "../memorypool.h"
#include "event.h"
#include <boost/bind.hpp>

class EventFactory : protected boost::noncopyable
{
public:
    typedef boost::shared_ptr< Event > EventPtr;

public:
    EventFactory();
    ~EventFactory();

    template< typename ET >
    EventPtr create()
    {
        void *p = m_pool.alloc(sizeof(ET) + sizeof(unsigned long));

        if(!p){
            LOG_ERROR() << "Alloc memory from memory pool.";

            return EventPtr();
        }

        *((unsigned long *)p) = sizeof(ET) + sizeof(unsigned long);

        EventPtr _new(new(p + sizeof(unsigned long)) ET(), boost::bind(&EventFactory::destroy, this, _1));
    }

protected:
    void destroy(Event *e);

protected:
    MemoryPool m_pool;
};

#endif // EVENTFACTORY_H
